﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using log4net;
using System.Reflection;

namespace Cn.Edwon.Data {
    public class ADO4MySql : AccessSupport, ICommonProcess, ICommonProcedure {
        public static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
         public ADO4MySql() { 
        
        }
         public ADO4MySql(String connString) {
            base.ConnectString = connString;
        }
         public ADO4MySql(String hostAddress, Int32 port, String dbName, String user, String password,String charset) {
             base.ConnectString = "server = " + hostAddress
             + "; user = " + user
             + "; database = " + dbName
             + "; port = " + port
             + "; password = " + password
             + "; character set = " + charset + ";";

         }

       #region ICommonProcess 成员
        public int CommonUpdateAction(string tsql) {
            this.Sql = tsql;
            log.Debug("tsql:[" + tsql + "]");
            using (MySqlConnection conn = new MySqlConnection(ConnectString)) {
                MySqlCommand odc = new MySqlCommand(tsql, conn);
                conn.Open();
                return odc.ExecuteNonQuery();
            }
        }

        public System.Data.DataSet CommonSelectAction(string tsql, string dataname) {
            this.Sql = tsql;
            log.Debug("tsql:[" + tsql + "]");
            using (MySqlConnection conn = new MySqlConnection(ConnectString)) {
                using (MySqlDataAdapter da = new MySqlDataAdapter(tsql, conn)) {
                    DataSet ds = new DataSet();
                    da.Fill(ds, dataname);
                    return ds;
                }
            }
        }

        #endregion

        #region ICommonProcedure 成员

         public void ExecuteProc(string rProcName, ref Object rOutObj, ref Object rReturnObj, params object[] rParams) {
            log.Debug("Proc:[" + rProcName + "]");
            using (MySqlConnection conn = new MySqlConnection(ConnectString)) {
                log.Debug("Database connect succeed");
                MySqlCommand command = new MySqlCommand(rProcName, conn);
                command.CommandType = CommandType.StoredProcedure;
                MySqlParameter rep = new MySqlParameter("ReturnValue", DbType.Object);
                rep.Direction = ParameterDirection.ReturnValue;
                command.Parameters.Add(rep);
                MySqlParameter oup = new MySqlParameter("OutValue", DbType.Object);
                oup.Direction = ParameterDirection.Output;
                command.Parameters.Add(oup);
                //
                if (rParams != null) {
                    if ((command.Parameters.Count - 1) != rParams.Length) {
                        throw new ArgumentNullException("The number of parameters does not match number of values for stored procedure.");
                    }
                    for (int i = 0; i < rParams.Length; i++) {
                        command.Parameters[i + 1].Value = (rParams[i] == null) ? DBNull.Value : rParams[i];
                    }
                }

                command.ExecuteNonQuery();
                rOutObj = command.Parameters["OutValue"];
                rReturnObj = command.Parameters["ReturnValue"];
            }
        }

        public void ExecuteProc(string rProcName, ref Object rOutObj, ref Object rReturnObj, IList<object> rParams) {
            log.Debug("Proc:[" + rProcName + "]");
            using (MySqlConnection conn = new MySqlConnection(ConnectString)) {
                log.Debug("Database connect succeed");
                MySqlCommand command = new MySqlCommand(rProcName, conn);
                command.CommandType = CommandType.StoredProcedure;
                MySqlParameter rep = new MySqlParameter("ReturnValue", DbType.Object);
                rep.Direction = ParameterDirection.ReturnValue;
                command.Parameters.Add(rep);
                MySqlParameter oup = new MySqlParameter("OutValue", DbType.Object);
                oup.Direction = ParameterDirection.Output;
                command.Parameters.Add(oup);
                //
                if (rParams != null) {
                    if ((command.Parameters.Count - 1) != rParams.Count) {
                        throw new ArgumentNullException("The number of parameters does not match number of values for stored procedure.");
                    }
                    for (int i = 0; i < rParams.Count; i++) {
                        command.Parameters[i + 1].Value = (rParams[i] == null) ? DBNull.Value : rParams[i];
                    }
                }

                command.ExecuteNonQuery();
                rOutObj = command.Parameters["OutValue"];
                rReturnObj = command.Parameters["ReturnValue"];
            }
        }

        #endregion
    }
}
